package com.example.weather.entity;

/**
 * @author
 *
 */

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.weather.enums.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable{

    @ApiModelProperty(value = "结果状态")
    private String code;

    @ApiModelProperty(value = "结果描述")
    private String message;

    @ApiModelProperty(value = "数据总数")
    private Long total;

    @ApiModelProperty(value = "存放异常信息，调试方便", hidden = true)
    private String exception;

    @ApiModelProperty(value = "业务数据")
    private T data;

    public boolean isSuccess() {
        return ObjectUtil.equals(code, ResultCode.SUCCESS.getCode());
    }

    public static <T> Result<T> success() {
        return new Result<T>().setCode(ResultCode.SUCCESS.getCode()).setMessage(ResultCode.SUCCESS.getMessage());
    }

    public static <T> Result<T> success(String message) {
        if (StrUtil.isBlank(message)) {
            message = ResultCode.SUCCESS.getMessage();
        }
        return new Result<T>().setCode(ResultCode.SUCCESS.getCode()).setMessage(message);
    }
    public static <T> Result<T> success(String message,Long total) {
        if (StrUtil.isBlank(message)) {
            message = ResultCode.SUCCESS.getMessage();
        }
        return new Result<T>().setCode(ResultCode.SUCCESS.getCode()).setMessage(message).setTotal(total);
    }

    public static <T> Result<T> data(String message, T data) {
        if (StrUtil.isBlank(message)) {
            message = ResultCode.SUCCESS.getMessage();
        }
        return new Result<T>().setCode(ResultCode.SUCCESS.getCode()).setMessage(message).setData(data);
    }

    public static <T> Result<T> data(T data) {
        return new Result<T>().setCode(ResultCode.SUCCESS.getCode()).setMessage(ResultCode.SUCCESS.getMessage()).setData(data);
    }

    public static <T> Result<T> fail(String message) {
        if (StrUtil.isBlank(message)) {
            message = ResultCode.FAIL.getMessage();
        }
        return new Result<T>().setCode(ResultCode.FAIL.getCode()).setMessage(message);
    }

    public static <T> Result<T> fail() {
        return new Result<T>().setCode(ResultCode.FAIL.getCode()).setMessage(ResultCode.FAIL.getMessage());
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return new Result<T>().setCode(resultCode.getCode()).setMessage(resultCode.getMessage());
    }

    public static <T> Result<T> fail(String code, String message) {
        if (StrUtil.isBlank(code)) {
            code = ResultCode.FAIL.getCode();
        }
        if (StrUtil.isBlank(message)) {
            message = ResultCode.FAIL.getMessage();
        }
        return new Result<T>().setCode(code).setMessage(message);
    }

    public static <T> Result<T> fail(Exception e) {
        if (e == null) {
            return Result.fail();
        }
        return new Result<T>().setCode(ResultCode.FAIL.getCode()).setMessage(ResultCode.FAIL.getMessage())
//                .setException(ExceptionUtil.stacktraceToString(e))
                ;
    }

    public static <T> Result<T> of(boolean isSuccess) {
        return isSuccess ? Result.success() : Result.fail();
    }

    public static <T> Result<T> of(boolean isSuccess, T data) {
        return isSuccess ? Result.data(data) : Result.fail();
    }
}
